<?php

namespace App\Http\Controllers;

use App\Models\Sect;
use App\Models\SectMember;
use Illuminate\Http\Request;

class SectMemberController extends Controller
{
    public function findSectByUser()
    {
        $userID = request()->input('user_id');
        $sectMember = SectMember::where('user_id', $userID)->first();
        if ($sectMember) {
            return response()->json(['recode' => 0,
                'sect' => [
                    'sect_id' => $sectMember->sect_id,
                    'sect_title' => $sectMember->title,
                    'sect_name' => $sectMember->sect->name,
                    'sect_short_name' => $sectMember->sect->short_name,
                ],
            ]);
        } else {
            return response()->json(['recode' => 0,
                'sect' => [
                    'sect_id' => 0,
                    'sect_title' => 100,
                    'sect_name' => '未加入门派',
                    'sect_short_name' => '未加入门派',
                ],
            ]);
        }
    }

    public function memberList()
    {
        $sectId = request()->input('sect_id');
        $list = SectMember::where('sect_id', $sectId)
            ->select('id', 'user_id', 'title')
            ->with('user:id,name,head_img,point,score,score_flag')
            ->orderBy('title')
            ->get();

        return response()->json([
            'recode' => 1,
            'list' => $list,
        ]);

    }

    public function leave()
    {
        $sectId = request()->input('sect_id');
        $userId = request()->input('user_id');
        \DB::transaction(function () use ($sectId, $userId) {
            //查询职位
            $sectMember = SectMember::where([['sect_id', $sectId], ['user_id', $userId]])->first();
            if ($sectMember->title == 10) {
                //转让帮主
                //查询职位最高人
                $otherSectMember = SectMember::where([['sect_id', $sectId], ['user_id', '!=', $userId]])
                    ->orderBy('title')
                    ->orderBy('created_at')
                    ->first();
                if ($otherSectMember) {
                    $otherSectMember->title = 10;
                    $otherSectMember->save();
                } else {
                    //删除门派
                    Sect::destroy($sectId);
                }
            }
            $sectMember->delete();

        });

        return response()->json([
            'recode' => 1,
            'msg' => '退出成功',
            'sect' => [
                'sect_id' => 0,
                'sect_title' => 100,
                'sect_name' => '未加入门派',
                'sect_short_name' => '未加入门派',
            ],
        ]);

    }

    public function info()
    {
        $sectId = request()->input('sect_id');
        $userId = request()->input('user_id');
        $sectMember = SectMember::where([['user_id', $userId], ['sect_id', $sectId]])
            ->select('id', 'user_id', 'sect_id', 'title')
            ->with('user:id,head_img,name')
            ->first();
        if ($sectMember == null) {
            return response()->json([
                'recode' => 0,
                'msg' => '资料错误',
            ]);
        }
        return response()->json([
            'recode' => 1,
            'info' => $sectMember,
        ]);

    }

    public function save()
    {
        $sectId = request()->input('sect_id');
        $userId = request()->input('user_id');
        $oprateId = request()->input('oprate_id');
        $title = request()->input('title');

        //查询人员信息
        $sectMember = SectMember::where([['user_id', $userId], ['sect_id', $sectId]])->first();
        //查询设置人员信息
        $masterSectMember = SectMember::where([['user_id', $oprateId], ['sect_id', $sectId]])->first();

        if ($title == 10) {
            //转让帮主
            if ($masterSectMember->title != 10) {
                return response()->json([
                    'recode' => 0,
                    'msg' => '设置失败',
                ]);
            }
            $masterSectMember->title = 20;
            $masterSectMember->save();
        }
        $sectMember->title = $title;
        $sectMember->save();

        return response()->json([
            'recode' => 1,
            'msg' => '更新成功',
            'title' => $masterSectMember->title,
        ]);

    }

    public function getPlaySect()
    {
        $playA = request()->input('playa');
        $playB = request()->input('playb');
        $sectA = SectMember::where('user_id', $playA)->first();
        $sectB = SectMember::where('user_id', $playB)->first();

        if ($sectA) {
            $retSectA['name'] = $sectA->sect->short_name . '-';
            $retSectA['title'] = $sectA->title;
        } else {
            $retSectA['name'] = "";
            $retSectA['title'] = 100;
        }
        if ($sectB) {
            $retSectB['name'] = $sectB->sect->short_name . '-';
            $retSectB['title'] = $sectB->title;
        } else {
            $retSectB['name'] = "";
            $retSectB['title'] = 100;
        }

        return response()->json([
            'recode' => 1,
            'sects' => ['secta' => $retSectA, 'sectb' => $retSectB],
        ]);

    }
}
